package com.xzzz.backend.bsms.server.rbac.user;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.xzzz.common.db.aspect.Pages;
import com.xzzz.common.base.exception.XzException400;
import com.xzzz.common.base.util.PrimaryKeyUtil;
import com.xzzz.common.base.util.security.BCryptUtil;
import com.xzzz.common.base.util.security.SaltUtil;
import com.xzzz.backend.bsms.server.rbac.user.pojo.UserEntity;
import com.xzzz.backend.bsms.server.rbac.user.pojo.UserPageReq;
import com.xzzz.backend.bsms.server.rbac.user.pojo.UserReq;
import com.xzzz.irda.auth.core.authorization.AuthContext;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserService extends ServiceImpl<UserMapper, UserEntity> {

    /**
     * 分页
     */
    @Pages
    public PageInfo<UserEntity> page(UserPageReq page) {
        return new PageInfo<>(this.listAll(page.to(UserReq.class)));
    }

    public List<UserEntity> listAll(UserReq req) {
        req.setDelTime(0L);
        return baseMapper.listAll(req.to(UserEntity.class));
    }

    public UserEntity selectById(Long id) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getId, id));
    }

    public UserEntity selectByUsername(String username) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, username));
    }

    public UserEntity selectByPhone(String phone) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, phone));
    }

    public UserEntity selectByOpenId(String openId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getOpenId, openId));
    }

    /**
     * 新增
     *
     * @param req 主体信息
     * @return 新增条数
     */
    @Transactional(rollbackFor = Exception.class)
    public Long insert(UserReq req) {
        UserEntity entity = req.to(UserEntity.class);
        entity.setId(PrimaryKeyUtil.nextId());
        if (StrUtil.isBlank(req.getUsername())) {
            entity.setUsername("U" + entity.getId());
        }
        if (StrUtil.isBlank(entity.getSalt())) {
            entity.setSalt(SaltUtil.randomSalt());
            entity.setPassword(BCryptUtil.encode("123", entity.getSalt()));
        }
        entity.setCreBy(AuthContext.getNameAndId());
        baseMapper.insert(entity);
        return entity.getId();
    }

    /**
     * 修改
     *
     * @param req 主体信息
     * @return 修改结果
     */
    @Transactional(rollbackFor = Exception.class)
    public Long update(UserReq req) {
        XzException400.throwBy(req.getId() == null, "ID不得为空");
        UserEntity upd = req.to(UserEntity.class);
        upd.setDelTime(null);// 防止在修改时删除
        upd.setUpdBy(AuthContext.getNameAndId());
        baseMapper.updById(upd);
        return req.getId();
    }

    /**
     * 删除
     *
     * @param id ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        XzException400.throwBy(ObjUtil.isNull(id), "ID不得为空");
        baseMapper.delLogic(id, AuthContext.getNameAndId(), System.currentTimeMillis());
    }

}
